# 导入模块
import time
from selenium import webdriver
from selenium.common.exceptions import InvalidArgumentException, WebDriverException, TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from common.functions import browser_operation


# TODO 创建Base类
class Base:
    """
    基础类,封装了selenium基础方法,供page包下面的类继承
    """

    # TODO 设置浏览器类型
    def __init__(self, browser):
        """
        启动浏览器
        browser:浏览器名称
        """
        self.driver = browser

    # TODO 请求目标网址
    def get_website(self, url):
        """
        url:请求网址
        两种异常：selenium.common.exceptions.WebDriverException: Message: unknown
                selenium.common.exceptions.WebDriverException: Message: unknown
        """
        try:
            self.driver.get(url)
        except (InvalidArgumentException, WebDriverException):
            print('网址不存在，重新输入网址')

    # TODO 定位元素, 返回元素
    def find_element(self, locator, timeout=3):
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located(locator))
        except TimeoutException:
            print('未获取到元素')

    # TODO 定位元素, 返回元素列表
    def find_elements(self, locator, timeout=3):
        try:
            return WebDriverWait(self.driver, timeout).until(EC.presence_of_all_elements_located(locator))
        except TimeoutException:
            print('未获取到元素')

    # TODO 获取标签文本
    def get_element_text(self, locator):
        element = self.find_element(locator)
        if element:
            return element.text

    # TODO 隐式等待
    def implicitly_wait(self, second=10):
        self.driver.implicitly_wait(second)

    # TODO 元素点击操作
    def click(self, locator):
        """
        locator: 定位的元素
        try:捕获异常
        判定定位到的元素是否为空
        """
        # 调用location_element 方法获取定位元素
        element = self.find_element(locator)
        if element is not None:
            element.click()
        else:
            print('元素定位失败')

    # TODO 获取元素属性
    def get_attr(self, locator, values='outerHTML'):
        """
        locator: 定位元素
        attribute: 元素属性
        """
        # 获取定位元素
        element = self.find_element(locator)
        # 打印出源代码
        if element:
            return element.get_attribute(values) if element.get_attribute(values) else print('元素不存在')
        else:
            print('未获取到元素')

    # TODO 下拉框操作01-Q
    def select_option(self,locator,index):
        """
        导入select类，创建对象，传入select标签对象
        通过索引选取option，索引从0开始
        """
        element = self.find_element(locator).click()
        select = Select(element)
        select.select_by_index(index)

    # TODO 下拉框操作02-Q
    def select(self, locator, num):
        select = Select(self.find_element(locator))
        return select.select_by_index(num)

    # TODO 下拉框操作03 1:索引 2:value值 3:文本-Q
    def get_select(self, locator, method, values):
        select = Select(self.find_element(locator))
        if select:
            if method == 1:
                return select.select_by_index(values)
            elif method == 2:
                return select.select_by_value(values)
            elif method == 3:
                return select.select_by_visible_text(values)
            else:
                print('没有此方法')
                
        # TODO 下拉框操作-04
    def select_box(self, locator, text):
        # 下拉框操作, 创建select对象, 参数是要操作的select标签
        s1 = self.find_element(locator)
        select = Select(s1)
        # 通过文本选取
        select.select_by_visible_text(text)

    # TODO 清空操作-05
    def clear(self, locator):
        # 清空文本
        element = self.find_element(locator)
        if element:
            element.clear()

    # TODO 写入信息方法-Q
    def send_key(self, locator, cons_name):
        element = self.find_element(locator)
        if element:
            element.send_keys(cons_name)

    # TODO 后退
    def retreat(self):
        """
        back:浏览器后退
        """
        self.driver.back()

    # TODO 前进
    def forward(self):
        """
        forward:浏览器前进
        """
        self.driver.forward()

    # TODO 刷新
    def refresh(self):
        """
        refresh:浏览器刷新
        """
        self.driver.refresh()

    # TODO 退出浏览器
    def quit(self, seconds=0):
        """
        退出浏览器
        seconds:设置停顿秒数，默认0s
        """
        # 设置停顿秒数
        time.sleep(seconds)
        self.driver.quit()

    # TODO 获取页面title
    def get_title(self):
        return self.driver.title

    # TODO 打开新的标签页
    def new_window(self, url):
        # url传参时, 要使用双重引号 "'xxx'"
        js = "window.open(" + "'" + url + "'" + ")"
        self.driver.execute_script(js)

    # TODO 获取弹窗文本信息
    def get_alert_text(self):
        alert = self.driver.switch_to.alert
        text = alert.text
        alert.accept()
        return text
